/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDScriptRunner.java
 *
 * Created on October 31, 2005, 8:26 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd;

import java.io.*;
import ssi_colorizer.*;
import ssi_socket.*;


/**
 *
 * @author John
 */
public class PTDScriptRunner implements PTDScriptComm, PTDDisplay{
    
    //private BufferedReader MyContent;
    protected PTDDisplay MyOutput;
    protected PTDScriptComm MyComm;
    protected PTDScriptThread MainThread;
    protected String ScriptPath;
    protected String Arguments;
    
    protected int doDisp;
    
    public boolean bDoDisplay = false;
    
    // -----------------------------------------------------    
    public PTDScriptRunner() {
        MyOutput = null;
        //MyContent = null;
        MyComm = null;
        ScriptPath = "";
        Arguments = "";
        doDisp = 1;
    }

    // -----------------------------------------------------    
    public String getNextLine(){
        // this function actually needs to get an input...
        if(MyComm != null){
            return MyComm.getNextLine();
        }
        return "";
    }
    
    // -----------------------------------------------------    
    public int display(String Output){
        if(doDisp > 0){
            MyOutput.display(Output);
        }
        return 1;
    }    
    
    public void setRunState(int newState){
        if(MyComm != null){
            MyComm.setRunState(newState);
        }
    }

    public boolean lineAvailable(){
        if(MyComm != null){
            return MyComm.lineAvailable();
        }
        return false;
    }    
    
    // -----------------------------------------------------    
    public void reportFault(int iCode, String Message){
        MyOutput.display(Message);
    }

    // -----------------------------------------------------    
    public void cleanUp(){
        MainThread.Kill();
    }
    
    // -----------------------------------------------------    
    public int loadFile(String FileName){
        try{
            File MyFile = new File(FileName);
            FileReader MyReader = new FileReader(MyFile);
            //MyContent = new BufferedReader(MyReader);
            ScriptPath = FileName;
            
        }catch(Exception e){
            // could not open file
            return 0;
        }
        return 1;
    }
    
    // -----------------------------------------------------    
    public void setOutput(PTDDisplay newOutput){
        MyOutput = newOutput;
    }

    // -----------------------------------------------------    
    public void setComm(PTDScriptComm newComm){
        MyComm = newComm;
    }
    
    // -----------------------------------------------------    
    public int startSuite(String Name){
        return MyOutput.startSuite(Name);
    }
    
    // -----------------------------------------------------    
    public int stopSuite(){
        return MyOutput.stopSuite();
    }

    // -----------------------------------------------------    
    public int stopTestcase(){
        return MyOutput.stopTestcase();
    }

    // -----------------------------------------------------    
    public int stopScript(){
        return MyOutput.stopScript();
    }
    
    public int startTestcase(String Name){
        return MyOutput.startTestcase(Name);
    }

    public int startScript(String Name){
        return MyOutput.startScript(Name);
    }    
    
    public int startProcessor(int Type, String Name){
        return MyOutput.startProcessor(Type, Name);
    }
 
    public int stopProcessor(){
        return MyOutput.stopProcessor();
    }


    
    // -----------------------------------------------------    
    public int getCurrentIndex(){
        return 0;
    }
    
    // -----------------------------------------------------    
    public void reset(){
        try{
            //MyContent.reset();
        }catch (Exception e){
            Main.debug_out(e.getMessage());
        }
    }
    
    // -----------------------------------------------------    
    public int setIndex(int New){
        return 0;
    }
    
    // -----------------------------------------------------    
    public int getIndex(){
        return 0;
    }
    
    // -----------------------------------------------------    
    public int run(boolean bWait, boolean bDebug, boolean bProcessor){
//        if(runState > 0){
//            PTDMessageBox.MessageBox("Only one script can be run at a time.  Please wait for other scripts to finish.");
//            return 0;
//        }
        
        // IS THIS FINE?
        if(bProcessor == false){
            if(PTDScriptThread.GlobalRunState == PTDScriptThread.THREAD_RUNNING){
                PTDMessageBox.MessageBox("Only one script can be run at a time.  Please wait for other scripts to finish.");
                MyOutput.display("Only one script can be run at a time.  Please wait for other scripts to finish.");
                return 0;            
            }
        }
        //MainThread = new PTDScriptThread(this, this, bDoDisplay);
        MainThread = new PTDScriptThread(this, MyOutput, bDoDisplay);
        //MainThread.init(PTDOptions.m_DefaultMachine, ssi_utilities.ssi_converter.IntFromString(PTDOptions.m_DefaultAgentPort), false);
        MainThread.init(false);
        MainThread.setScript(ScriptPath);
        MainThread.setArguments(Arguments);
        MainThread.bDebug = bDebug;
        MainThread.bProcessor = bProcessor;
        MainThread.start();
        setRunState(1);
        if(bWait){
           try{
               Thread.currentThread().sleep(1000);
           }catch (Exception e){
               Main.debug_out("Error in thread sleep");
           }
            
            while(MainThread.getRunState() == MainThread.THREAD_RUNNING){
                try{
                    Thread.currentThread().sleep(10);
                }catch (Exception e){
                    Main.debug_out("Error in thread sleep");
                }
            }
        }
    
        setRunState(0);       
        return 1;
    }

    public int displayFormatted(String Output){
        display(Output);
        return 1;
    }
    public int displayError(String Output){
        //display(Output);
        MyComm.displayError(Output);
        return 1;        
    }
    public int displayWarning(String Output){
        //display(Output);
        MyComm.displayWarning(Output);
        return 1;        
    }
    
    public int displayFailure(String Output){
        //display(Output);
        MyComm.displayFailure(Output);
        return 1;        
    }
    
    public int requestDebug(String LineInfo){
        if(MyComm == null){
            return -1;
        }
        return MyComm.requestDebug(LineInfo);
    }
    
     public String getDebugCmd(){
        if(MyComm == null){
            return "";
        }
        return MyComm.getDebugCmd();
    }

    public int displayDebug(String Output){
        if(MyComm == null){
            return -1;
        }
        return MyComm.displayDebug(Output);
    }
    
    public void setArguments(String inArgs){
        Arguments = inArgs;
    }

        
}
